var express = require('express');
var router = express.Router();
var connection = require('../connect');
/* GET home page. */

// 验证用户名和密码是否重复
router.post('/check', (req, resp, next) => {
  const { phone, uname } = req.body
  const sql = 'SELECT COUNT(*) AS count FROM user WHERE phone=? or uname=?'
  connection.query(
    sql,
    [phone, uname],
    (err, r) => {
      if (err) {
        return next(err);
      }
      // resp.send({
      //   msg: '通',
      //   data: r
      // })
      // console.log(data[0].count);
      if (r[0].count == 0) {
        resp.send({ code: 200, msg: '可以使用' })
      } else {
        resp.send({ code: 202, msg: '用户名或手机号重复' })
      }
    },
  )
})
// 注册
router.post('/userin', (req, resp, next) => {
  const { pwd, uname, pname, ename, gender, email, phone } = req.body
  const args = [pwd, uname, pname, ename, gender, email, phone]
  for (let i = 0; i < args.length; i++) {
    if (args[i] == '') {
      resp.send({
        code: 202,
        msg: '不能为空',
      })
    }

  }
  connection.query(
    'INSERT INTO user(pwd,uname,pname,ename,gender,email,phone) VALUES(MD5(?),?,?,?,?,?,?)',
    args,
    (err, r) => {
      if (err) {
        return next(err);
      }
      resp.send({
        msg: '注册成功',
      })
    },
  )
})

module.exports = router;
